package cn.tedu.charging.common.utils;


import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();

    // 将对象转换为JSON字符串 序列化
    public static String toJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // 将JSON字符串转换为对象 反序列化

    /**
     * 把JSON字符串转换为Java对象
     * @param json 具体的JSON字符串
     * @param clazz 对象的类型
     * @return
     * @param <T>
     */
    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
